﻿/******************************************************************************
* File description: animationController_Practice.cpp
* Function: 打靶动画Controller，基于State的状态，根据时间计算当前动画的过渡状态，并提供获取当前状态的接口
* Current Version: 1.0.0
* Author: JNN
* Date: 2025-07-01
*  -----
*  Copyright by 博坤生物 .
 *****************************************************************************/

#include "animationController_Practice.h"

/** @brief 构造函数 */
AnimationController_Practice::AnimationController_Practice(AnimationState_Practice* state)
{
    _state = state;
    startTimer();
    reset();
    connect(_state, SIGNAL(reset()), this, SLOT(reset()));
}


/** @brief 重置动画状态至机器复位状态 */
void AnimationController_Practice::reset(){

    _feedPortPosi = 0;
    _feedPortState = 0;
    _wheelAngle = 1;
    _wheelState = 0;
    _barPosi = 1;
    _barState = 0;
}


/** @brief 获取当前帧所有机械模块的位置/状态
 *  @return QVariantMap返回每个类型的值
 */
QVariantMap AnimationController_Practice::requestInfo(){

    QVariantMap res;

    res["FeedPortPosi"]  = _feedPortPosi;
    res["FeedPortState"] = _feedPortState;
    res["WheelAngle"]    = _wheelAngle;
    res["WheelState"]    = _wheelState;
    res["BarPosi"]       = _barPosi;
    res["BarState"]      = _barState;
    res["Pump1State"]    = _state->_pump1State;
    res["Pump2State"]    = _state->_pump2State;
    res["PumpN2State"]   = _state->_pumpN2State;
    res["BlowTargetState"] = _state->_blowTargetState;
    res["BrakeState"]    = _state->_brakeState;
    res["AbsorbState"]   = _state->_absorbState;

//    res["DrawTargetBottle"] = StaticProperties::practiceTargetStatus["0"]=="无" ? false : true;
    res["DrawTargetBottle"] = true;

    QVariantList bulbState;
    for(int i = 0; i < 4; i ++){
        if(StaticProperties::pipeStatus.size() <= i){
            bulbState.push_back(-1);
        }else{
            bulbState.push_back(StaticProperties::pipeStatus[QString::number(i)]);
        }
    }
    res["BulbState"]     = bulbState;

    return res;
}

/** @brief 启动计时器，在开启计时器后动画会在每个interval时间调用updateState重新计算状态
 */
void AnimationController_Practice::startTimer(){

    if(_timer){
        delete _timer;
        _timer = nullptr;
    }

    _timer = new QTimer();
    _timer->setInterval(_interval);
    connect(_timer, SIGNAL(timeout()), this, SLOT(updateState()));
    _timer->start();

}

/** @brief 关闭计时器
 */
void AnimationController_Practice::stopTimer(){

    if(_timer){
        _timer->stop();
        delete _timer;
        _timer = nullptr;
    }
}

/** @brief 状态更新函数，根据参数计算每个模块的状态
 * @param interval 距离上一次调用的间隔，传入-1时使用默认间隔值（一般为30ms）。传入其他值时，启动快速动画模式。
 */
void AnimationController_Practice::updateState(double interval){

    if(_state == nullptr){
        __LOG_ERROR("State=nullptr");
        return;
    }

    double speedBase;
    if(interval == -1){
        speedBase = _interval / 1000.0;
    }else{
        speedBase = 1;
    }

    double costTime;

    // 进料口
    costTime = 1;
    if(_feedPortPosi != _state->_feedPortPosi){
        _feedPortState = 1;
        if(_feedPortPosi > _state->_feedPortPosi){
            _feedPortPosi = std::max(_feedPortPosi-speedBase/costTime, (double)_state->_feedPortPosi);
        }else{
            _feedPortPosi = std::min(_feedPortPosi+speedBase/costTime, (double)_state->_feedPortPosi);
        }
    }else{
        _feedPortState = 0;
    }

//    // 转轮角度
//    costTime = 1.5;
//    if(_wheelAngle != _state->_wheelAngle){
//        _wheelState = 1;
//        if(_wheelAngle > _state->_wheelAngle){
//            _wheelAngle = std::max(_wheelAngle-speedBase/costTime, (double)_state->_wheelAngle);
//        }else{
//            _wheelAngle = std::min(_wheelAngle+speedBase/costTime, (double)_state->_wheelAngle);
//        }
//    }else{
//        _wheelState = 0;
//    }

//    // bar的左右位置
//    costTime = 2;
//    if(_barPosi != _state->_barPosi){
//        _barState = 1;
//        if(_barPosi > _state->_barPosi){
//            _barPosi = std::max(_barPosi-speedBase/costTime, (double)_state->_barPosi);
//        }else{
//            _barPosi = std::min(_barPosi+speedBase/costTime, (double)_state->_barPosi);
//        }
//    }else{
//        _barState = 0;
//    }



    // Bar的队列
    // 当前为空时，判断还有没有待执行的步骤
    if(_curBarOp == AS_P::EMPTY){
        if(_state->_barOperation.size() > 0){
            _curBarOp = _state->_barOperation.dequeue();
        }
    }
    // 开始执行
    switch (_curBarOp) {
    // - 左移->0
    case AS_P::MOV_LEFT:{
        costTime = 1;
        if(_barPosi > 0){
            _barState = 1;
            _barPosi = std::max(_barPosi-speedBase/costTime, 0.0);
        }else{
            _barState = 0;
            _curBarOp = AS_P::EMPTY;
        }
    }   break;
    // - 右移
    case AS_P::MOV_RIGHT:{
        costTime = 1;
        if(_barPosi < 1){
            _barState = 1;
            _barPosi = std::min(_barPosi+speedBase/costTime, 1.0);
        }else{
            _barState = 0;
            _curBarOp = AS_P::EMPTY;
        }
    }   break;
    // - 左转轮->0
    case AS_P::WHEEL_TURN_LEFT:{
        costTime = 1;
        if(_wheelAngle > 0){
            _wheelState = 1;
            _wheelAngle = std::max(_wheelAngle-speedBase/costTime, 0.0);
        }else{
            _wheelState = 0;
            _curBarOp = AS_P::EMPTY;
        }
    }   break;
    // - 下转轮->1
    case AS_P::WHEEL_TURN_DOWN:{
        costTime = 1;
        if(_wheelAngle < 1){
            _wheelState = 1;
            _wheelAngle = std::min(_wheelAngle+speedBase/costTime, 1.0);
        }else{
            _wheelState = 0;
            _curBarOp = AS_P::EMPTY;
        }
    }   break;
    // - 锁定
    case AS_P::LOCK:{
        _state->_brakeState = 1;
        _curBarOp = AS_P::EMPTY;
    }   break;
    // - 取消锁定
    case AS_P::UNLOCK: {
        _state->_brakeState = 0;
        _curBarOp = AS_P::EMPTY;
    }   break;
    default:
        break;
    }

}
